if SERVER then
	AddCSLuaFile("shared.lua")
end

SWEP.HoldType = "shotgun"

if CLIENT then
	SWEP.PrintName = "870"
	SWEP.Slot = 2
	-- SWEP.ViewModelFlip = false
end

SWEP.Base = "weapon_gun_base"
SWEP.Primary.Recoil	= 2.5
SWEP.Primary.Damage = 12
SWEP.Primary.Delay = 0.95
SWEP.Primary.MaxCone = 0.15
SWEP.Primary.Cone = 0.095
SWEP.Primary.MinCone = 0.095
SWEP.Primary.ClipSize = 8
SWEP.Primary.Automatic = false
SWEP.Primary.DefaultClip = 8
SWEP.Primary.ClipMax = 32
SWEP.Primary.Ammo = "Buckshot"
SWEP.Primary.NumShots = 8
SWEP.Knockback = 1100

SWEP.ViewModel = "models/weapons/v_shot_870marine.mdl"
SWEP.WorldModel	= "models/weapons/w_shot_m3super90.mdl"

SWEP.Primary.Sound = Sound("Weapon_M3.Single")

SWEP.IronSightsPos = Vector(1.72, -0.493, 0.68)
SWEP.IronSightsAng = Vector(0, 0, 0)

SWEP.ReloadTimer = 0

function SWEP:SetupDataTables()
   self:DTVar("Bool", 0, "reloading")

   return self.BaseClass.SetupDataTables(self)
end

function SWEP:ShootBullet(dmg, rec, num, cone)
	num = num or 1
	cone = cone or 0.01
	
	local owner = self.Owner
	
	owner:MuzzleFlash()
	
	local bullet 	= {}
	bullet.Num 		= num
	bullet.Src 		= owner:GetShootPos()
	bullet.Dir 		= (owner:EyeAngles() + self.Owner:GetPunchAngle()):Forward()
	bullet.Spread 	= Vector(cone, cone, 0)
	bullet.Tracer	= 4
	bullet.Force	= 100
	bullet.Damage 	= dmg
	
	owner:FireBullets(bullet)
	self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
	owner:SetAnimation(PLAYER_ATTACK1)	
	
	if ((SinglePlayer() and SERVER) or ((not SinglePlayer()) and CLIENT and IsFirstTimePredicted())) then
		rec = self:GetSights() and (rec * 0.6) or rec

		local eyeang = self.Owner:EyeAngles()
		eyeang.pitch = eyeang.pitch - rec * self.ConeAffect
		self.Owner:SetEyeAngles(eyeang)
	end
end

function SWEP:Reload()
	self:SetSights(false)
   
	if self.dt.reloading then return end

	if not IsFirstTimePredicted() then return end
   
	if self.Weapon:Clip1() < self.Primary.ClipSize and self.Owner:GetAmmoCount(self.Primary.Ammo) > 0 then
		if self:StartReload() then
			return
		end
	end
end

function SWEP:StartReload()
	if self.dt.reloading then
		return false
	end

	if not IsFirstTimePredicted() then return false end

	self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
   
	local ply = self.Owner
	local wep = self.Weapon
	
	if not ply or ply:GetAmmoCount(self.Primary.Ammo) <= 0 then 
		return false
	end
   
	if wep:Clip1() >= self.Primary.ClipSize then 
		return false 
	end

	wep:SendWeaponAnim(ACT_SHOTGUN_RELOAD_START)

	self.ReloadTimer =  CurTime() + wep:SequenceDuration()

	self.dt.reloading = true

	return true
end

function SWEP:PerformReload()
	local ply = self.Owner
	local wep = self.Weapon
   
	wep:SetNextPrimaryFire( CurTime() + self.Primary.Delay )

	if not ply or ply:GetAmmoCount(self.Primary.Ammo) <= 0 then return end

	if wep:Clip1() >= self.Primary.ClipSize then return end

	ply:RemoveAmmo(1, self.Primary.Ammo, false)
	wep:SetClip1(self.Weapon:Clip1() + 1)

	wep:SendWeaponAnim(ACT_VM_RELOAD)

	self.ReloadTimer = CurTime() + wep:SequenceDuration()
end

function SWEP:FinishReload()
	self.dt.reloading = false
	self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_FINISH)
	
	self.ReloadTimer = CurTime() + self.Weapon:SequenceDuration()
end

function SWEP:CanPrimaryAttack()
	if self.Weapon:Clip1() <= 0 then
		self:EmitSound("Weapon_Shotgun.Empty")
		self:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
		return false
	end
	return true
end

function SWEP:SecondaryThink()
	if self.dt.reloading and IsFirstTimePredicted() then
		if self.Owner:KeyDown(IN_ATTACK) then
			self:FinishReload()
			return
		end
		
		if self.ReloadTimer <= CurTime() then
			if self.Owner:GetAmmoCount(self.Primary.Ammo) <= 0 then
				self:FinishReload()
			elseif self.Weapon:Clip1() < self.Primary.ClipSize then
				self:PerformReload()
			else
				self:FinishReload()
			end
			return            
		end
	end
end

function SWEP:Deploy()
	self.dt.reloading = false
	self.ReloadTimer = 0
	return self.BaseClass.Deploy(self)
end
